<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceRequestList" @selection-change="handleSelectionChange">
      <el-table-column label="服务内容" align="center" prop="serviceRequestContent" />
      <el-table-column label="完成情况" align="center" prop="isDone" width="180" :formatter="statusFormat"/>
      <el-table-column label="事件" align="center" prop="event" />
      <el-table-column label="服务时间" align="center" prop="serviceRequestTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.serviceRequestTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hotel:serviceRequest:remove']"
          >取消</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="服务内容">
          <el-input v-model="form.serviceRequestContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="服务时间" prop="serviceRequestTime">
          <el-date-picker clearable
            v-model="form.serviceRequestTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServiceRequest, getServiceRequest, delServiceRequest, addServiceRequest, updateServiceRequest,getListNow,addService,cancelMyRequest } from "@/api/hotel/serviceRequest";

export default {
  name: "ServiceRequest",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      serviceRequestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkinId: undefined,
        serviceRequestContent: undefined,
        serviceRequestTime: undefined,
        isDone:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceRequestContent: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        serviceRequestTime: [
          { required: true, message: "不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      getListNow().then(response => {
        this.serviceRequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serviceRequestId: undefined,
        checkinId: undefined,
        serviceRequestContent: undefined,
        serviceRequestTime: undefined,
        isDone:undefined
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "提出服务需求";
    },

    submitForm() {

          this.buttonLoading = true;
            addService(this.form).then(response => {
              this.$modal.msgSuccess("提出成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
              this.cancel();
            });
            
    },


    /** 取消按钮操作 */
    handleDelete(row) {
      if (row.isDone=="w") {
        this.$modal.confirm('是否确认取消该服务需求').then(() => {
          this.loading = true;
          return cancelMyRequest(row);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("取消成功");
        }).catch(() => {
        }).finally(() => {
          this.loading = false;
        });
      }
      else{
        this.$modal.confirm("只能取消在等待的需求");
      }
      
    },
    statusFormat(row,column){
      if(row.isDone=="w"){
        return "等待处理";
      }
      else if(row.isDone=="n"){
        return "无法处理";
      }
      else if(row.isDone=="y"){
        return "已处理";
      }
      else if(row.isDone=="q"){
        return "已取消";
      }
      else if(row.isDone=="g"){
        return "正在处理";
      }
    }
    
  }
};
</script>
